package com.authine.cloudpivot.ext.kingdee.work;


import com.alibaba.cola.dto.SingleResponse;
import com.authine.cloudpivot.ext.dto.kingDee.KingDeeBusiness;
import com.authine.cloudpivot.ext.dto.kingDee.request.FAccountIdEntity;
import com.authine.cloudpivot.ext.dto.kingDee.request.FDetailIdEntity;
import com.authine.cloudpivot.ext.dto.kingDee.request.FEntity;
import com.authine.cloudpivot.ext.dto.kingDee.request.KingDeeEntity;
import com.authine.cloudpivot.ext.dto.voucher.Voucher;
import com.authine.cloudpivot.ext.enums.kingdee.AccountingDimensionEnum;
import com.authine.cloudpivot.ext.enums.kingdee.BusinessType;
import com.authine.cloudpivot.ext.enums.kingdee.FormEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class VoucherWork extends BaseWork {



    // 单表查询 云枢支持
//    @Path("pageVoucher")
//    @POST
//    @Operation(summary = "查询凭证表",tags = CUSTOM_SERVICE)
//    public SingleResponse<List<Voucher>> pageVoucher(@RequestBody VoucherQuery query){
//
//        return  null;
//    }



    public SingleResponse<List<Voucher>> listVoucher( String time){
        List<Voucher> voucherList = voucherRepository.queryByDateStr(time);
        SingleResponse singleResponse = SingleResponse.buildSuccess();
        singleResponse.setData(voucherList);
        return  singleResponse;
    }



    public SingleResponse<Integer> pushVoucher( List<Voucher> vouchers){
        //todo 需要添加事务
        log.info("生成凭证-K3  request:{}", vouchers);

        List<FEntity> fEntityList = new ArrayList<>();

        List<String> idList = vouchers.stream().map(Voucher::getId).collect(Collectors.toList());

        //todo 根据id 获取对应数据 这里查询逻辑不是特别理解
        List<Voucher> query = voucherRepository.queryByIds(idList);

        log.info("生成凭证-K3 符合条件数据  query:{}", query);

        if (CollectionUtils.isEmpty(query)) {
            log.warn(" 生成凭证-K3 <<<<< 没有找到需要处理凭证数据 >>>>>");
            SingleResponse singleResponse = SingleResponse.buildSuccess();
            singleResponse.setData(0);
            return singleResponse;
        }

        query.forEach(e->{

            FEntity fEntity1 = FEntity.addFentity();

            if (StringUtils.isNotBlank(e.getRemark())) {
                fEntity1.setFExplanation(e.getRemark());
            }

            if (StringUtils.isNotBlank(e.getSubject_coding())) {
                fEntity1.setFAccountId(FAccountIdEntity.faccount(e.getSubject_coding()));
            }

            if (StringUtils.isNotBlank(e.getAd_coding())) {

                if (e.getAd_status() == AccountingDimensionEnum.Vendor) {
                    fEntity1.setFDetailIdEntity(FDetailIdEntity.addFDetailID4(e.getAd_coding()));
                }

                if (e.getAd_status() == AccountingDimensionEnum.Customer) {
                    fEntity1.setFDetailIdEntity(FDetailIdEntity.addDetailID6(e.getAd_coding()));
                }
            }

            if (e.getDebit_amount().compareTo(BigDecimal.ZERO) !=0) {

                fEntity1.setFDebit(e.getDebit_amount());
            }

            if (e.getCredit_amount().compareTo(BigDecimal.ZERO) !=0) {
                fEntity1.setFCredit(e.getCredit_amount());
            }

            fEntityList.add(fEntity1);

        });

        voucherRepository.deleteByIds(idList);

        Voucher voucher = vouchers.get(0);

        KingDeeBusiness business = new KingDeeBusiness(voucher.getBusiness_id(),"", BusinessType.OrderTransfer,voucher.getCreatedTime(),null,voucher.getAccount_name());

        KingDeeEntity kingDeeEntity = getKingDeeEntity(fEntityList, business);

        doSaveKingDeeAndRecord(FormEnum.VOUCHER,kingDeeEntity,business);

        SingleResponse singleResponse = SingleResponse.buildSuccess();
        singleResponse.setData(idList.size());
        log.info("生成凭证-K3  业务逻辑处理结束 <<<");

        return singleResponse;
    }
}
